/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-6-8
 */
package com.autotag.train;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import fanglu.util.FileDealer;

public class ManualTagReader {
	public static void main(String args[]){
		printInfo();
	}
	public static void printInfo(){
		HashMap<String, ArrayList<String>> uuidMapFunc = new HashMap<String, ArrayList<String>>();
		HashMap<String, ArrayList<String>> uuidMapDomain = new HashMap<String, ArrayList<String>>();
		readTag("C:/Users/ftc/Desktop/ICSOC/final_data/serviceList(1-150).txt", 150, uuidMapFunc, uuidMapDomain);
		Iterator<String> uuid_itor = uuidMapFunc.keySet().iterator();
		int func_total = 0;
		int domain_total = 0;
		while(uuid_itor.hasNext()){
			String thisUUID = uuid_itor.next();
			int thisFuncCount = uuidMapFunc.get(thisUUID).size();
			int thisDomainCount = uuidMapDomain.get(thisUUID).size();
			System.out.println(thisUUID + "," + thisFuncCount + "," + thisDomainCount);
			func_total += thisFuncCount;
			domain_total += thisDomainCount;
		}
		System.out.println("total," + func_total + "," + domain_total);
	}
	
	public static void readTag(String filename, int total, 
			HashMap<String, ArrayList<String>> uuidMapFunc, 
			HashMap<String, ArrayList<String>> uuidMapDomain){
		try {
			String content = FileDealer.getFileContent(filename);
			StringReader sr = new StringReader(content);
			BufferedReader br = new BufferedReader(sr);
			String thisUUID = "";
			int type = 0;
			int count = 0;
			String oneLine = br.readLine();
			ArrayList<String> thisFuncList = new ArrayList<String>();
			ArrayList<String> thisDomainList = new ArrayList<String>();
			HashSet<String> funcSet = new HashSet<String>();
			HashSet<String> domainSet = new HashSet<String>();
			while (oneLine != null){
				oneLine = oneLine.trim();
				if (oneLine.startsWith("@")){
					type = 0;
				}else if (oneLine.startsWith("#")){
					thisUUID = new String(oneLine.substring(1));
					type = 0;
					++ count;
					if (count >= total){
						break;
					}
					thisFuncList = new ArrayList<String>();
					thisDomainList = new ArrayList<String>();
					uuidMapFunc.put(thisUUID, thisFuncList);
					uuidMapDomain.put(thisUUID, thisDomainList);
					funcSet = new HashSet<String>();
					domainSet = new HashSet<String>();
				}else if (oneLine.startsWith("functionality tag:")){
					type = 1;
				}else if (oneLine.startsWith("domain tag:")){
					type = 2;
				}else{
					if (type == 1){
						String [] sa = oneLine.split(",");
						for (int i=0; i<sa.length; ++i){
							if (sa[i].length() > 0 && funcSet.add(sa[i])){
								thisFuncList.add(sa[i].toLowerCase().trim());
							}
						}
					}else if (type == 2){
						String [] sa = oneLine.split(",");
						for (int i=0; i<sa.length; ++i){
							if (sa[i].length() > 0 && domainSet.add(sa[i])){
								thisDomainList.add(sa[i].toLowerCase().trim());
							}
						}
					}
				}
				oneLine = br.readLine();
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
